package com.cspg.ssss.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cspg.ssss.config.WeixinProperties;
import com.cspg.ssss.domain.DepartmentEntity;
import com.cspg.ssss.domain.UserDetailInfoEntity;
import com.cspg.ssss.service.WeixinService;
import com.cspg.ssss.service.WxDepartmentService;
import com.cspg.ssss.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wxapi")
public class WxDepartmentController {

    @Autowired
    WeixinProperties wxProperties;

    @Autowired
    WeixinService wxService;

    @Autowired
    WxDepartmentService wxDepartmentService;

    @GetMapping("/getDepartAndUsersByDepart")
    public ResultVo getDepartAndUsersByDepart(String departid){
        int depart = 1;
        if(departid != null){
            depart = Integer.parseInt(departid);
        }
        HashMap<String,Object> curcondition = new HashMap<String, Object>();
        curcondition.put("did",depart);
        List<DepartmentEntity> curdepart = wxDepartmentService.getDepartmentListByCondition(curcondition);

        HashMap<String,Object> condition = new HashMap<String, Object>();
        condition.put("pid",depart);

        List<DepartmentEntity> departList = wxDepartmentService.getDepartmentListByCondition(condition);

        HashMap<String,Object> usercondition = new HashMap<String, Object>();
        usercondition.put("did",depart);

        List<UserDetailInfoEntity> userList = wxDepartmentService.getDepartmentUserList(usercondition);

        DepartmentAndUserListVo retObjVo = new DepartmentAndUserListVo();
        if(curdepart != null && curdepart.size() == 1){
            DepartmentEntity en = curdepart.get(0);
            DepartmentVo tmpVo = new DepartmentVo();
            tmpVo.setId(en.getId());
            tmpVo.setPid(en.getPid());
            tmpVo.setDepartment_name(en.getDepartment_name());
            tmpVo.setDepartment_path(en.getDepartment_path());
            tmpVo.setDepartment_pathname(en.getDepartment_pathname());
            tmpVo.setOrder(en.getOrder());
            retObjVo.setCurr_depart(tmpVo);
        }else {
            retObjVo.setCurr_depart(null);
        }

        List<DepartmentVo> vo_departList = new ArrayList<DepartmentVo>();
        List<UserInfoVo> vo_userList = new ArrayList<UserInfoVo>();
        if(departList != null && departList.size() > 0){
            for(int i=0;i<departList.size();i++){
                DepartmentEntity en = departList.get(i);
                DepartmentVo tmpVo = new DepartmentVo();
                tmpVo.setId(en.getId());
                tmpVo.setPid(en.getPid());
                tmpVo.setDepartment_name(en.getDepartment_name());
                tmpVo.setDepartment_path(en.getDepartment_path());
                tmpVo.setDepartment_pathname(en.getDepartment_pathname());
                tmpVo.setOrder(en.getOrder());

                vo_departList.add(tmpVo);
            }
        }
        if(userList != null && userList.size() > 0){
            for(int i=0;i<userList.size();i++){
                UserDetailInfoEntity en = userList.get(i);
                UserInfoVo tmpVo = new UserInfoVo();
                tmpVo.setUser_id(en.getUser_id());
                tmpVo.setUser_name(en.getUser_name());
                tmpVo.setMobile(en.getMobile());
                tmpVo.setEmail(en.getEmail());
                tmpVo.setDepartment_name(en.getDepartment_name());
                tmpVo.setDepartment_path(en.getDepartment_path());
                tmpVo.setDepartment_displaypath(en.getDepartment_displaypath());

                vo_userList.add(tmpVo);
            }
        }
        retObjVo.setDepart_list(vo_departList);
        retObjVo.setUser_list(vo_userList);

        ResultVo retVo = ResultVoUtil.success("success",retObjVo);
        return  retVo;
    }

    @GetMapping("/getDepartAndUsersByDepartPath")
    public ResultVo getDepartAndUsersByDepartPath(String departpath){
        HashMap<String,Object> curcondition = new HashMap<String, Object>();
        curcondition.put("department_pathname",departpath);
        List<DepartmentEntity> curdepart = wxDepartmentService.getDepartmentListByCondition(curcondition);
        if(curdepart != null && curdepart.size() == 1){
            return getDepartAndUsersByDepart(String.valueOf(curdepart.get(0).getId()));
        }

        ResultVo retVo = ResultVoUtil.error("9999","根据路径获取部门失败！");
        return  retVo;

    }

    @GetMapping("/searchUserByName")
    public ResultVo searchUserByName(String user_name){
        HashMap<String,Object> usercondition = new HashMap<String, Object>();
        usercondition.put("user_name",user_name);

        List<UserDetailInfoEntity> userList = wxDepartmentService.getDepartmentUserList(usercondition);
        List<UserInfoVo> vo_userList = new ArrayList<UserInfoVo>();
        if(userList != null && userList.size() > 0){
            for(int i=0;i<userList.size();i++){
                UserDetailInfoEntity en = userList.get(i);
                UserInfoVo tmpVo = new UserInfoVo();
                tmpVo.setUser_id(en.getUser_id());
                tmpVo.setUser_name(en.getUser_name());
                tmpVo.setMobile(en.getMobile());
                tmpVo.setEmail(en.getEmail());
                tmpVo.setDepartment_name(en.getDepartment_name());
                tmpVo.setDepartment_path(en.getDepartment_path());
                tmpVo.setDepartment_displaypath(en.getDepartment_displaypath());

                vo_userList.add(tmpVo);
            }
        }

        ResultVo retVo = ResultVoUtil.success("success",vo_userList);

        return  retVo;
    }

    @PostMapping("/getsubdept")
    public ResultVo searchUserByName(@RequestBody Map<String,String> condition){
        String uid = condition.get("userid");
        String dept = condition.get("dept");
        DepartVo ret = wxDepartmentService.getUserSubDept(uid,dept);

        ResultVo retVo = ResultVoUtil.success("success",ret);

        return  retVo;
    }

}
